package com.bjpowernode.common.client;


import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;


/*redisde的工具类*/
@Component
public class RedisClient {

    private StringRedisTemplate tpl;
    private HashOperations<String ,String,String> hashOperations;

    private ZSetOperations<String ,String> zSetOperations;

    private ValueOperations<String ,String> valueOperations;



    public RedisClient(StringRedisTemplate stringRedisTemplate){
        this.tpl=stringRedisTemplate;
        zSetOperations=tpl.opsForZSet();
        valueOperations = tpl.opsForValue();
        hashOperations = tpl.opsForHash();

    }

    //创建ZSet类型,初始数据到ZSet
    public boolean addZSet(String key,String value, double score){

      Boolean add = zSetOperations.add(key,value,score);
      return add !=null && add;
    }


    //短信验证string类型的方法
    public boolean addStr(String key, String value ,int minute){
        valueOperations.set(key,value,minute, TimeUnit.MINUTES);
       Boolean exists= tpl.hasKey(key);
       return exists !=null && exists;
    }




    //从ZSet,按照score降序查询
    public Set<ZSetOperations.TypedTuple<String>> reverseRangeWithScore(String key, long start, long end ){
       return  zSetOperations.reverseRangeWithScores(key, start, end);
    }
    //从ZSet,按照score升序查询
    public Set<ZSetOperations.TypedTuple<String>> rangeWithScore(String key, long start, long end ){
       return  zSetOperations.rangeWithScores(key, start, end);
    }


    //更新投资排行榜,更新ZSet中某个phone的score
    public void incrementScore(String key,String value,double score){
        zSetOperations.incrementScore(key,value,score);
    }



    //验证码
    public String getStr(String key){
       return valueOperations.get(key);
    }




    //hash的add
    public boolean addHash(String key , Map<String,String> data,int minute){
        hashOperations.putAll(key,data);
        //设置有效时间,60分钟
            Boolean expire=true;
        if (minute > 0){
       expire= tpl.expire(key,minute,TimeUnit.MINUTES);
        }
        return expire != null && expire;
    }



    //获取hssh中某个field的值
    public String getHashField(String key, String field){
      return   hashOperations.get(key,field);

    }



    //incr.
    public Long incr(String key){
         return valueOperations.increment(key);
    }









    public boolean hasKey(String key) {
       Boolean bool= tpl.hasKey(key);
       return bool != null&& bool;
    }


    //从redis中删除处理过的订单号
    public boolean deleteValueZSet(String payOrderId, String orderId) {
        Long remove=zSetOperations.remove(payOrderId,orderId);
        return remove != null && remove == 1;
    }


    //获取hash类型的所有值
    public Map<String,String> getHashAllFiel(String key) {
        return hashOperations.entries(key);
    }


    //退出登录,删除key
    public boolean deleteKey(String key){
        Boolean bool=tpl.delete(key);
        return bool != null && bool;
    }
}
